/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpe.cin.modcs.util;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.SimpleTableModel;
import org.netbeans.microedition.lcdui.TableItem;

/**
 * @author fcfm
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command inputCommand;
    private Command outputCommand;
    private Command matrixSizeCommand;
    private Command matrixSizeCommand1;
    private Command okCommand;
    private Form MainMenu;
    private StringItem stringItem;
    private Form Input;
    private TableItem tableItem;
    private Form Output;
    private TableItem tableItem1;
    private TextBox configureOutputSize;
    //</editor-fold>//GEN-END:|fields|0|
    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
    // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
    // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getMainMenu());//GEN-LINE:|3-startMIDlet|1|3-postAction
    // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
    // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
    // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
    // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == Input) {//GEN-BEGIN:|7-commandAction|1|31-preAction
            if (command == matrixSizeCommand) {//GEN-END:|7-commandAction|1|31-preAction
            // write pre-action user code here
//GEN-LINE:|7-commandAction|2|31-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|19-preAction
        } else if (displayable == MainMenu) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|3|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|19-postAction
            // write post-action user code here
            } else if (command == inputCommand) {//GEN-LINE:|7-commandAction|5|24-preAction
                // write pre-action user code here
                switchDisplayable(null, getInput());//GEN-LINE:|7-commandAction|6|24-postAction
            // write post-action user code here
            } else if (command == outputCommand) {//GEN-LINE:|7-commandAction|7|26-preAction
                // write pre-action user code here
                switchDisplayable(null, getOutput());//GEN-LINE:|7-commandAction|8|26-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|35-preAction
        } else if (displayable == Output) {
            if (command == matrixSizeCommand1) {//GEN-END:|7-commandAction|9|35-preAction
                // write pre-action user code here
                switchDisplayable(null, getConfigureOutputSize());//GEN-LINE:|7-commandAction|10|35-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|43-preAction
        } else if (displayable == configureOutputSize) {
            if (command == okCommand) {//GEN-END:|7-commandAction|11|43-preAction
            // write pre-action user code here
//GEN-LINE:|7-commandAction|12|43-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|7-postCommandAction
        }//GEN-END:|7-commandAction|13|7-postCommandAction
    // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|14|
    //</editor-fold>//GEN-END:|7-commandAction|14|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
        // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: MainMenu ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of MainMenu component.
     * @return the initialized component instance
     */
    public Form getMainMenu() {
        if (MainMenu == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            MainMenu = new Form("Welcome", new Item[] { getStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
            MainMenu.addCommand(getExitCommand());
            MainMenu.addCommand(getInputCommand());
            MainMenu.addCommand(getOutputCommand());
            MainMenu.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
        // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return MainMenu;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            stringItem = new StringItem("Hello", "Hello, World!");//GEN-LINE:|16-getter|1|16-postInit
        // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|16-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: inputCommand ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of inputCommand component.
     * @return the initialized component instance
     */
    public Command getInputCommand() {
        if (inputCommand == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            inputCommand = new Command("Incidence Matrix", Command.ITEM, 0);//GEN-LINE:|23-getter|1|23-postInit
        // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return inputCommand;
    }
    //</editor-fold>//GEN-END:|23-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: outputCommand ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of outputCommand component.
     * @return the initialized component instance
     */
    public Command getOutputCommand() {
        if (outputCommand == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            outputCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|25-getter|1|25-postInit
        // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return outputCommand;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: matrixSizeCommand ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of matrixSizeCommand component.
     * @return the initialized component instance
     */
    public Command getMatrixSizeCommand() {
        if (matrixSizeCommand == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            matrixSizeCommand = new Command("Matrix Size", Command.ITEM, 0);//GEN-LINE:|30-getter|1|30-postInit
        // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return matrixSizeCommand;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: matrixSizeCommand1 ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of matrixSizeCommand1 component.
     * @return the initialized component instance
     */
    public Command getMatrixSizeCommand1() {
        if (matrixSizeCommand1 == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            matrixSizeCommand1 = new Command("Item", Command.ITEM, 0);//GEN-LINE:|34-getter|1|34-postInit
        // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return matrixSizeCommand1;
    }
    //</editor-fold>//GEN-END:|34-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Input ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of Input component.
     * @return the initialized component instance
     */
    public Form getInput() {
        if (Input == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            Input = new Form("form1", new Item[] { getTableItem() });//GEN-BEGIN:|27-getter|1|27-postInit
            Input.addCommand(getMatrixSizeCommand());
            Input.setCommandListener(this);//GEN-END:|27-getter|1|27-postInit
        // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return Input;
    }
    //</editor-fold>//GEN-END:|27-getter|2|
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Output ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initiliazed instance of Output component.
     * @return the initialized component instance
     */
    public Form getOutput() {
        if (Output == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            Output = new Form("form", new Item[] { getTableItem1() });//GEN-BEGIN:|32-getter|1|32-postInit
            Output.addCommand(getMatrixSizeCommand1());
            Output.setCommandListener(this);//GEN-END:|32-getter|1|32-postInit
        // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return Output;
    }
    //</editor-fold>//GEN-END:|32-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tableItem ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initiliazed instance of tableItem component.
     * @return the initialized component instance
     */
    public TableItem getTableItem() {
        if (tableItem == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            tableItem = new TableItem(getDisplay(), "tableItem");//GEN-LINE:|36-getter|1|36-postInit
        // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return tableItem;
    }
    //</editor-fold>//GEN-END:|36-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|42-getter|1|42-postInit
            // write post-init user code here
            int lines = Integer.parseInt(configureOutputSize.getString().substring(0, configureOutputSize.getString().indexOf(",")));
            int cols = Integer.parseInt(configureOutputSize.getString().substring(configureOutputSize.getString().indexOf(","), configureOutputSize.getString().length()));
        }//GEN-BEGIN:|42-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|42-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: configureOutputSize ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of configureOutputSize component.
     * @return the initialized component instance
     */
    public TextBox getConfigureOutputSize() {
        if (configureOutputSize == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            configureOutputSize = new TextBox("Matrix Size (#lines, #cols)", null, 100, TextField.ANY);//GEN-BEGIN:|39-getter|1|39-postInit
            configureOutputSize.addCommand(getOkCommand());
            configureOutputSize.setCommandListener(this);//GEN-END:|39-getter|1|39-postInit
        // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return configureOutputSize;
    }
    //</editor-fold>//GEN-END:|39-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tableItem1 ">//GEN-BEGIN:|44-getter|0|44-preInit
    /**
     * Returns an initiliazed instance of tableItem1 component.
     * @return the initialized component instance
     */
    public TableItem getTableItem1() {
        if (tableItem1 == null) {//GEN-END:|44-getter|0|44-preInit
            // write pre-init user code here
            tableItem1 = new TableItem(getDisplay(), "tableItem1");//GEN-LINE:|44-getter|1|44-postInit
            // write post-init user code here
        }//GEN-BEGIN:|44-getter|2|
        return tableItem1;
    }
    //</editor-fold>//GEN-END:|44-getter|2|
    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
